using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class Upgrade : MonoBehaviour
{
	private GameObject _currentStick;

	private GameObject _opponentStick;

	public List<StickUpgradeLevels> _customStickLevels;

	[SerializeField]
	private GameObject _railsStick;

	[SerializeField]
	private GameObject[] _stickLevels;

	[SerializeField]
	private GameObject[] _opponentStickPrefabs;

	public Vector3 _stickPosition = new Vector3(0.58f, 0f, 0f);

	public Quaternion _stickRotation = Quaternion.identity;

	private int _lastPlayerLength = -1;

	private int _lastOpponentLength = -1;

	private void Awake()
	{
		if ((Preloader.Transition.IsEnabled || Preloader.EnhancedIncremental.IsEnabled) && _customStickLevels.Count > 0)
		{
			int index = SingletonMB<GameManager>.Instance.Currentlevel % _customStickLevels.Count;
			_stickLevels = _customStickLevels[index]._stickLevels.ToArray();
		}
	}

	private void Start()
	{
		InventoryManager instance = InventoryManager.Instance;
		instance.OnSkinChanged = (Action)Delegate.Combine(instance.OnSkinChanged, new Action(UpdateAllStage));
		if (SingletonMB<GameManager>.Instance.IsDuelMode)
		{
			DuelModeManager instance2 = SingletonMB<DuelModeManager>.Instance;
			instance2.OnUpgradedPlayer = (Action)Delegate.Combine(instance2.OnUpgradedPlayer, new Action(PlayerUpgraded));
			DuelModeManager instance3 = SingletonMB<DuelModeManager>.Instance;
			instance3.OnUpgradedOpponent = (Action)Delegate.Combine(instance3.OnUpgradedOpponent, new Action(OpponentUpgraded));
			DuelModeManager instance4 = SingletonMB<DuelModeManager>.Instance;
			instance4.OnDuelStarted = (Action)Delegate.Combine(instance4.OnDuelStarted, new Action(RefreshOpponentStick));
		}
	}

	public void UpdateAllStage()
	{
		if (SingletonMB<GameManager>.Instance.IsDuelMode)
		{
			PlayerUpgraded();
			RefreshOpponentStick();
			StartCoroutine(WaitAndRefresh());
		}
		else
		{
			UpdateLong();
			UpdateSize();
			UpdatePower();
			UpdateOil();
		}
	}

	private IEnumerator WaitAndRefresh()
	{
		yield return null;
		RefreshOpponentStick();
	}

	public void UpdateLong()
	{
		if (Preloader.Transition.IsEnabled && _customStickLevels.Count > 0)
		{
			int index = SingletonMB<GameManager>.Instance.Currentlevel % _customStickLevels.Count;
			_stickLevels = _customStickLevels[index]._stickLevels.ToArray();
		}
		bool flag = false;
		flag = ((!(SingletonMB<RvDrillBoost>.Instance != null)) ? (SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive && Preloader.BonusLevelConfig.UpgradedMachine) : ((SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive && Preloader.BonusLevelConfig.UpgradedMachine) || SingletonMB<RvDrillBoost>.Instance.bonusActive));
		if ((bool)_currentStick)
		{
			UnityEngine.Object.Destroy(_currentStick);
		}
		int num = NintendoPlayerPrefs.GetInt("Long", 0);
		if (flag)
		{
			num = 4;
		}
		if (!SingletonMB<DuelModeManager>.Instance && !SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive && Preloader.RailDigging.IsEnabled)
		{
			_currentStick = UnityEngine.Object.Instantiate(_railsStick);
			_currentStick.transform.position = _stickPosition;
			_currentStick.transform.localRotation = _stickRotation;
			SingletonMB<GameManager>.Instance.Control.GetComponentInChildren<RailSizeHandler>().UpdateSize();
		}
		else
		{
			for (int i = 0; i < _stickLevels.Length; i++)
			{
				if (i == num)
				{
					_currentStick = UnityEngine.Object.Instantiate(_stickLevels[i]);
					_currentStick.transform.position = _stickPosition;
					_currentStick.transform.localRotation = _stickRotation;
				}
			}
		}
		UpdateSize();
	}

	public void UpdateSize(bool max = false)
	{
		bool flag = (SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive && Preloader.BonusLevelConfig.UpgradedMachine) || SingletonMB<RvDrillBoost>.Instance.bonusActive;
		Transform child = _currentStick.transform.GetChild(1);
		TryGetComponent<MultipleSawsUpgrade>(out var _);
		float num = 0f;
		if (!max && !flag)
		{
			num = ((!Preloader.BetterUpgradeScaling.IsEnabled) ? ((0.35f + (float)NintendoPlayerPrefs.GetInt("Size", 0) * 0.05f) * (1f + (float)NintendoPlayerPrefs.GetInt("ScinSize", 0) / 100f)) : ((0.6f + (float)NintendoPlayerPrefs.GetInt("Size", 0) * 0.025f) * (1f + (float)NintendoPlayerPrefs.GetInt("ScinSize", 0) / 100f)));
			if (SingletonMB<GameManager>.Instance.IsDuelMode)
			{
				num = 0.35f + (float)SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Size * 0.05f;
			}
			UnityEngine.Object.FindObjectOfType<Control>().SetHead(child.GetComponent<Rigidbody>());
			UnityEngine.Object.FindObjectOfType<Cam>().SetHead(child.GetComponent<Transform>());
			UnityEngine.Object.FindObjectOfType<PowerTimer>()._head = child.GetComponent<Transform>();
		}
		else
		{
			num = 0.85f;
		}
		UnityEngine.Object.FindObjectOfType<Control>().SetHead(child.GetComponent<Rigidbody>());
		UnityEngine.Object.FindObjectOfType<Cam>().SetHead(child.GetComponent<Transform>());
		UnityEngine.Object.FindObjectOfType<PowerTimer>()._head = child.GetComponent<Transform>();
		child.localScale = new Vector3(num, num, 0.4f);
		UpdatePower(max);
	}

	private void SetupStickPower(GameObject stick, bool max = false)
	{
		Head component = stick.transform.GetChild(1).GetComponent<Head>();
		Transform child = stick.transform.GetChild(1);
		component.TryGetComponent<MultipleSawsUpgrade>(out var component2);
		bool flag = SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive && Preloader.BonusLevelConfig.UpgradedMachine;
		bool bonusActive = SingletonMB<RvDrillBoost>.Instance.bonusActive;
		if (!max && !flag && !bonusActive)
		{
			int num = NintendoPlayerPrefs.GetInt("Power", 0) * 2 * (1 + (int)((float)NintendoPlayerPrefs.GetInt("ScinPower", 0) / 100f));
			if (Preloader.BetterUpgradeScaling.IsEnabled)
			{
				num = 8 + (int)((float)NintendoPlayerPrefs.GetInt("Power", 0) * 1.5f) * (1 + (int)((float)NintendoPlayerPrefs.GetInt("ScinPower", 0) / 100f));
			}
			if (num > 29)
			{
				num = 29;
			}
			component.amount = num;
			CapsuleCollider component3 = child.GetComponent<CapsuleCollider>();
			component3.radius = 0.7f - 0.07f * (float)NintendoPlayerPrefs.GetInt("Size", 0);
			if (component3.radius < 0.3f)
			{
				component3.radius = 0.3f;
			}
			if (SingletonMB<GameManager>.Instance.IsDuelMode)
			{
				component.amount = SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Size;
				component3.radius = 0.7f - 0.07f * (float)SingletonMB<Shop>.Instance.MaxSizeLevel;
				if (component3.radius < 0.3f)
				{
					component3.radius = 0.3f;
				}
			}
		}
		else if (!max && !flag && bonusActive && SingletonMB<DuelModeManager>.Instance == null)
		{
			component.amount = 17;
			child.GetComponent<CapsuleCollider>().radius = 0.4f;
		}
		else
		{
			component.amount = 29;
			child.GetComponent<CapsuleCollider>().radius = 0.3f;
			if (SingletonMB<GameManager>.Instance.IsDuelMode)
			{
				component.amount = SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Size;
				CapsuleCollider component4 = child.GetComponent<CapsuleCollider>();
				component4.radius = 0.7f - 0.07f * (float)SingletonMB<Shop>.Instance.MaxSizeLevel;
				if (component4.radius < 0.3f)
				{
					component4.radius = 0.3f;
				}
				component2?.UpdatePoower(component4.radius);
			}
		}
		component.UpdateRotationSpeed();
	}

	public void UpdatePower(bool max = false)
	{
		SetupStickPower(_currentStick, max);
		if (SingletonMB<GameManager>.Instance.IsDuelMode && _opponentStick != null)
		{
			SetupStickPower(_opponentStick, max);
		}
	}

	public void UpdateOil()
	{
		SingletonMB<GameManager>.Instance.currentOilUI.UpdateValues();
	}

	public void MaxPower()
	{
		UpdateSize(max: true);
	}

	public void CurrentPower()
	{
		UpdateSize();
	}

	public void PlayerUpgraded()
	{
		UpdatePlayerLength();
		UpdatePlayerSize();
	}

	public void OpponentUpgraded()
	{
		UpdateOpponentLength();
		UpdateOpponentSize();
	}

	public void UpdatePlayerLength()
	{
		if (Preloader.Transition.IsEnabled && _customStickLevels.Count > 0)
		{
			int index = SingletonMB<GameManager>.Instance.Currentlevel % _customStickLevels.Count;
			_stickLevels = _customStickLevels[index]._stickLevels.ToArray();
		}
		if (SingletonMB<GameManager>.Instance.IsDuelMode && _lastPlayerLength == SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Length)
		{
			return;
		}
		_lastPlayerLength = SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Length;
		if ((bool)_currentStick)
		{
			UnityEngine.Object.Destroy(_currentStick);
		}
		int @int = NintendoPlayerPrefs.GetInt("Long", 0);
		@int = SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Length;
		if (Preloader.RailDigging.IsEnabled)
		{
			_currentStick = UnityEngine.Object.Instantiate(_railsStick);
			_currentStick.transform.position = new Vector3(0.58f, 0f, 0f);
		}
		else
		{
			for (int i = 0; i < _stickLevels.Length; i++)
			{
				if (i == @int)
				{
					_currentStick = UnityEngine.Object.Instantiate(_stickLevels[i]);
					_currentStick.transform.position = new Vector3(0.58f, 0f, 0f);
				}
			}
		}
		Head component = _currentStick.transform.GetChild(1).GetComponent<Head>();
		UnityEngine.Object.FindObjectOfType<Control>().SetHead(component.GetComponent<Rigidbody>());
		UnityEngine.Object.FindObjectOfType<Cam>().SetHead(component.GetComponent<Transform>());
		UnityEngine.Object.FindObjectOfType<PowerTimer>()._head = component.GetComponent<Transform>();
		Head[] array = UnityEngine.Object.FindObjectsOfType<Head>();
		for (int j = 0; j < array.Length; j++)
		{
			array[j].SetKinematic(active: false);
		}
	}

	public void RefreshOpponentStick()
	{
		_lastOpponentLength = SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Length;
		if ((bool)_opponentStick)
		{
			UnityEngine.Object.Destroy(_opponentStick);
		}
		int length = SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Length;
		for (int i = 0; i < _opponentStickPrefabs.Length; i++)
		{
			if (i == length)
			{
				_opponentStick = UnityEngine.Object.Instantiate(_opponentStickPrefabs[i]);
				_opponentStick.transform.position = new Vector3(7.3f, 0f, 0f);
			}
		}
		Head[] array = UnityEngine.Object.FindObjectsOfType<Head>();
		for (int j = 0; j < array.Length; j++)
		{
			array[j].SetKinematic(active: false);
		}
		UpdateOpponentSize();
	}

	public void UpdateOpponentLength()
	{
		if (SingletonMB<GameManager>.Instance.IsDuelMode && _lastOpponentLength == SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Length)
		{
			return;
		}
		_lastOpponentLength = SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Length;
		if ((bool)_opponentStick)
		{
			UnityEngine.Object.Destroy(_opponentStick);
		}
		int length = SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Length;
		for (int i = 0; i < _opponentStickPrefabs.Length; i++)
		{
			if (i == length)
			{
				_opponentStick = UnityEngine.Object.Instantiate(_opponentStickPrefabs[i]);
				_opponentStick.transform.position = new Vector3(7.3f, 0f, 0f);
			}
		}
		Head[] array = UnityEngine.Object.FindObjectsOfType<Head>();
		for (int j = 0; j < array.Length; j++)
		{
			array[j].SetKinematic(active: false);
		}
	}

	public void UpdatePlayerSize(bool max = false)
	{
		Transform child = _currentStick.transform.GetChild(1);
		float num = 0f;
		if (!max)
		{
			num = 0.35f + (float)NintendoPlayerPrefs.GetInt("Size", 0) * 0.05f;
			if (SingletonMB<GameManager>.Instance.IsDuelMode)
			{
				num = 0.35f + (float)SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Size * 0.05f;
			}
			UnityEngine.Object.FindObjectOfType<Control>().SetHead(child.GetComponent<Rigidbody>());
			UnityEngine.Object.FindObjectOfType<Cam>().SetHead(child.GetComponent<Transform>());
			UnityEngine.Object.FindObjectOfType<PowerTimer>()._head = child.GetComponent<Transform>();
		}
		else
		{
			num = 0.85f;
		}
		child.localScale = new Vector3(num, num, 0.4f);
		UpdatePower(max);
	}

	public void UpdateOpponentSize(bool max = false)
	{
		float num = 0f;
		if (SingletonMB<GameManager>.Instance.IsDuelMode)
		{
			num = 0.35f + (float)SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Size * 0.05f;
			Transform child = _opponentStick.transform.GetChild(1);
			child.localScale = new Vector3(num, num, 0.4f);
			UnityEngine.Object.FindObjectOfType<OpponentControl>().SetHead(child.GetComponent<Rigidbody>());
			if (!SingletonMB<DuelModeManager>.Instance.DuelStarted)
			{
				SingletonMB<GameManager>.Instance.Cam.custom = SingletonMB<DuelModeManager>.Instance.DuelCameraPosition;
				SingletonMB<GameManager>.Instance.Cam.customFOV = 90f;
			}
		}
		UpdatePower(max);
	}
}
